package com.project.controller;

import com.project.bean.SaleCarBean;
import com.project.service.ISaleCarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("saleCar")
public class SaleCarController {
    @Autowired
    private ISaleCarService service;

    @RequestMapping("add")
    public String add(Integer productId,Integer loginUserId) {
        SaleCarBean saleCar=service.findByProductId(productId);
        if (saleCar==null){
            service.add(productId, loginUserId);
        }else {
            service.updateNumById(saleCar.getId(),saleCar.getNum()+1);
        }
        return "ok";
    }

    @RequestMapping("updateNumById")
    public String updatePriceAndNumById(Integer saleCarId, Integer newNum) {
        service.updateNumById(saleCarId, newNum);
        return "ok";
    }

    @RequestMapping("delById")
    public String delById(Integer saleCarId) {
        service.delById(saleCarId);
        return "ok";
    }

    @RequestMapping("delByUser")
    public String delByUser(Integer userId) {
        service.delByUser(userId);
        return "ok";
    }

    @RequestMapping("findByUserId")
    public List<SaleCarBean> findByUserId(Integer userId){
        return service.findByUserId(userId);
    }

}
